package io.gitee.chearnee.fw.datasecurity.core.executor.desensitization;

import io.gitee.chearnee.fw.datasecurity.core.util.DbCryptUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/10/2022 4:02 PM
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SDExecutorFactory {

    private static SDExecutor<Object> emptySdExecutor = null;
    private static SDExecutor<Iterable<?>> iterableSdExecutor = null;
    private static SDExecutor<Object> arraySdExecutor = null;
    private static SDExecutor<Object> beanSdExecutor = null;
    private static SDExecutor<String> stringSdExecutor = null;

    public static SDExecutor<Object> getEmptySdExecutor() {
        return emptySdExecutor;
    }

    public static SDExecutor<Iterable<?>> getIterableSdExecutor() {
        return iterableSdExecutor;
    }

    public static SDExecutor<Object> getArraySdExecutor() {
        return arraySdExecutor;
    }

    public static SDExecutor<Object> getBeanSdExecutor() {
        return beanSdExecutor;
    }

    public static SDExecutor<String> getStringSdExecutor() {
        return stringSdExecutor;
    }

    public static SDExecutor<?> getSDExecutor(Object obj) {
        if (obj == null || DbCryptUtil.inIgnoreClass(obj.getClass()) || obj instanceof Map) {
            return emptySdExecutor;
        }
        if (obj instanceof Iterable) {
            return iterableSdExecutor;
        }
        if (obj instanceof String) {
            return stringSdExecutor;
        }
        if (obj.getClass().isArray()) {
            return arraySdExecutor;
        }
        return beanSdExecutor;
    }

    public static void initExecutorFactory() {
        emptySdExecutor = new EmptySDExecutor();
        iterableSdExecutor = new IterableSDExecutor();
        arraySdExecutor = new ArraySDExecutor();
        beanSdExecutor = new BeanSDExecutor();
        stringSdExecutor = new StringSDExecutor();
    }

    public static void destroy() {
        emptySdExecutor = null;
        iterableSdExecutor = null;
        arraySdExecutor = null;
        beanSdExecutor = null;
        stringSdExecutor = null;
    }

}
